Angle Between the Hands of a Clock

Understand and solve the interview question "Angle Between the Hands of a Clock."

Description#

Given two numbers, hour and minutes we will return the smaller angle (in degrees) formed between the hour and the minute hands on a clock.

Let’s look at some examples to better understand this:

Created with Fabric.js 3.6.6
At hour = 3 and minutes = 60, angle = 75

1 of 2

Created with Fabric.js 3.6.6
At hour = 3 and minutes = 15, angle = 7.5

2 of 2

Coding exercise#

Angle between hands of a clock

Solution#

The idea is to separately calculate the angles between the 0-minutes vertical line and each hand. The answer will be the difference that is found between the two angles.

Note: Black lines in the clock represent the 0-minutes vertical lines.

The minute hand points at 12 minutes on the clock while the hour hand points at 1 hour on the clock

Minute hand angle

Let’s start from the minute hand. The minute hand moves with 1 min intervals. At 1 min, the angle of the minute hand is . . Since the whole circle of the clock is equal to 360° degrees and 60 minutes, we estimated that the minute hand moves 1 min = 360° / 60 = 6° degree with each minute.

60 min = 360°
60 min = 360°
1 min = 6°
1 min = 6°
Viewer does not support full SVG 1.1
At 60 minutes, the angle of the clock is 360°, and at 1 minute the angle is 6°

Now, we can easily find an angle between the 0-minutes vertical line and a minute hand, using minutes_angle = minutes X 6°.

10 min = 10 X 6° = 60°
10 min = 10 X 6° = 60°
50 min = 50 X 6° = 300°
50 min = 50 X 6° = 300°
Viewer does not support full SVG 1.1
At 10 minutes, the angle of the clock is 60°, and at 50 minutes, the angle is 300°

Hour hand angle

The hour hand moves with 1-hour intervals. The whole circle is equal to 360° degrees and 12 hours. So, the hour hand moves 1 hour = 360°/ 12 =30° degrees with each hour.

12 hours = 360°
12 hours = 360°
1 hour = 360° / 12 = 30°
1 hour = 360° / 12 = 30°
Viewer does not support full SVG 1.1
At 12 hours, the angle of the clock is 360°, and at 1 hour, the angle is 30°

In the case of minutes = 0, we can easily find an angle between a 12-hour vertical line and an hour hand. We can use the following formula for this case: hour_angle = hour X 30°

11 hour = 11 X 30° = 330
11 hour = 11 X 30° = 330
1 hour = 1 X 30° = 30°
1 hour = 1 X 30° = 30°
Viewer does not support full SVG 1.1
At 11 hours, the angle of the clock is 330°, and at 1 hour, the angle is 30°

In the case of a 12-hour vertical line, the actual angle is zero. Therefore, we need to correct the expression to make it hour_angle = (hour mod 12) X 30°

Let’s consider another case, where minutes are greater than 0 (minutes > 0). In this case, we also have to consider the additional movement of the hour hand, where the hour hand moves forward slightly and jumps between the integer values, following the movement of the minute hand.

hour_angle = (hour mod 12 + minutes / 60) X 30°

(2 + 30 / 60) X 30° = 75°
(2 + 30 / 60) X 30° = 75°
(1 + 42 / 60) X 30° = 45°
(1 + 42 / 60) X 30° = 45°
Viewer does not support full SVG 1.1
At 2 hour and 30 minutes, the angle of the clock is 75°. At 1 hour and 42 minutes, the angle is 45°

Algorithm#

Here is how we will implement this feature:

  • First, we will initialize the constants, one_min_angle and one_hour_angle, with the following values respectively: 6, 30.

  • Then, we will initialize the minutes_angle with the minutes_angle = one_min_angle * minutes formula to find the angle between the minute hand and the 0-minutes vertical line.

  • Next, we will initialize the hour_angle with the hour_angle = (rem(hour, 12) + minutes / 60) * one_hour_angle formula to find the angle between the hour hand and the 12-hour vertical line.

  • We will find the difference, using difference = abs(hour_angle - minutes_angle).

  • At the end, we will return the smallest angle: min(difference, 360 - difference).

Let’s look at the code for this solution:

Angle between hands of a clock

Complexity measures#

Time complexity Space complexity
O(1)O(1) O(1)O(1)

Time complexity#

The algorithm will have O(1)O(1) time complexity.

Space complexity#

The algorithm will have O(1)O(1) space complexity.

Pacific Atlantic Water Flow

Where to Go from Here?